package com.fun_ordering.service.impl;

import com.fun_ordering.service.CustomerInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.CustomerInfoMapper;
import com.fun_ordering.pojo.CustomerInfo;
import com.fun_ordering.pojo.CustomerInfoExample;
import com.fun_ordering.pojo.CustomerInfoExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 顾客信息表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {

	@Autowired
	private CustomerInfoMapper customerInfoMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<CustomerInfo> findAll() {
		return customerInfoMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<CustomerInfo> page=   (Page<CustomerInfo>) customerInfoMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(CustomerInfo customerInfo) {
		customerInfoMapper.insert(customerInfo);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(CustomerInfo customerInfo){
		customerInfoMapper.updateByPrimaryKey(customerInfo);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param cusId
	 * @return
	 */
	@Override
	public CustomerInfo findOne(String cusId){
		return customerInfoMapper.selectByPrimaryKey(cusId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(String[] cusIds) {
		for(String cusId:cusIds){
			customerInfoMapper.deleteByPrimaryKey(cusId);
		}		
	}
	
	
		@Override
	public PageResult findPage(CustomerInfo customerInfo, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		CustomerInfoExample example=new CustomerInfoExample();
		Criteria criteria = example.createCriteria();
		
		if(customerInfo!=null){			
						if(customerInfo.getNickName()!=null && customerInfo.getNickName().length()>0){
				criteria.andNickNameLike("%"+customerInfo.getNickName()+"%");
			}			if(customerInfo.getCusPhone()!=null && customerInfo.getCusPhone().length()>0){
				criteria.andCusPhoneLike("%"+customerInfo.getCusPhone()+"%");
			}	
		}
		
		Page<CustomerInfo> page= (Page<CustomerInfo>)customerInfoMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
